package com.qf.netty.channel;

import io.netty.channel.Channel;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @Description:保持所有客户端的连接
 * @Author: xbb
 * @Date:2020/4/21
 */
public class ChannelGroup {

    /**
     * key:设备id
     * channel:设备id的连接对象
     */
    public static Map<String, Channel> channelMap = new HashMap<>();


    public static void addChannel(String did,Channel channel){
        channelMap.put(did,channel);
    }

    //获取
    public static Channel getChannel(String did){
        return channelMap.get(did);
    }

    //删除
    public static void removeChannel(String did){ channelMap.remove(did);
    }

    public static void removeChannel(Channel channel){
        if(channelMap.containsValue(channel)){
            Set<Map.Entry<String, Channel>> entries = channelMap.entrySet();
            for(Map.Entry<String, Channel> entry:entries){
                if(entry.getValue()==channel){
                    channelMap.remove(entry.getKey());
                    break;
                }
            }
        }
    }

}
